package com.vvlive.caibao.smartcity.ui;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.app.SmartCityApp;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.view.CommonActionBar;
import com.vvlive.caibao.smartcity.view.RoundImageView;

import org.kymjs.kjframe.ui.BindView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/7/14.
 * 设置页面
 */
public class SettingsActivity extends BaseActivity implements OnItemClickListener {

    @BindView(id = R.id.settings_personal, click = true)
    private View mPersonal;

    @BindView(id = R.id.cc_details_avatar)
    private RoundImageView mAvatar;  // 头像
    @BindView(id = R.id.cc_ahead_details_name)
    private TextView mAheadName;
    @BindView(id = R.id.cc_ahead_details_type)
    private TextView mAheadNumber;
    @BindView(id = R.id.cc_details_name)
    private TextView mName;    // 姓名
    @BindView(id = R.id.cc_details_type)
    private TextView mNumber;    // 账号
    @BindView(id = R.id.settings_list)
    private ListView mList;  // 设置列表

//    @BindView(id = R.id.settings_button, click = true)
//    Button logOut;  // 退出登录

    /**
     * 设置条目图标
     */
    private int icons[] = {
            R.mipmap.settings_account_manage,
            R.mipmap.settings_private,
            R.mipmap.settings_notification,
            R.mipmap.settings_order_list,
            R.mipmap.settings_invite_friends,
            R.mipmap.settings_about_us,
            R.mipmap.settings_check_for_updates
    };

    /**
     * 设置条目标题
     */
    private String[] items = {"账号管理", "隐私设置", "通知", "我的订单", "邀请好友", "关于我们", "检查更新"};

    /**
     * 账号管理
     */
    private final static int ACCOUNT = 0;
    /**
     * 隐私设置
     */
    private final static int PRIVATE = 1;
    /**
     * 通知
     */
    private final static int NOTIFY = 2;
    /**
     * 我的订单
     */
    private final static int ORDER = 3;
    /**
     * 邀请好友
     */
    private final static int INVITE = 4;
    /**
     * 关于我们
     */
    private final static int ABOUT_US = 5;
    /**
     * 检查更新
     */
    private final static int CHECK = 6;

    private final String[] KEYS = { "icon", "title" };
    private final int[] VALUES = { R.id.settings_icon, R.id.settings_title };


    @Override
    protected int getContentViewRes() {
        return R.layout.activity_settings;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = getCommonActionBar();
        actionBar.setTitle("设置");
        actionBar.setMoreVisible(false);

        mAheadName.setText("姓名-");
        mAheadNumber.setText("账号-");
        mName.setText("Black Widow");
        mNumber.setText("54567613165756");

        SimpleAdapter adapter = new SimpleAdapter(this, getSettings(), R.layout.item_settings_list, KEYS, VALUES);
        mList.setAdapter(adapter);
        mList.setOnItemClickListener(this);
    }

    /**
     * 获得设置
     *
     * @return  设置
     */
    private List<Map<String, Object>> getSettings() {
        List<Map<String, Object>> data = new ArrayList<>();
        for (int i = 0; i < icons.length; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("icon", icons[i]);
            map.put("title", items[i]);
            data.add(map);
        }
        return data;
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()) {
            case R.id.settings_button:  // 退出登录
                toast("退出登录");
                newActivity(SignInActivity.class);
                finish();
                SmartCityApp.isLogin = false;
                SmartCityApp.currentUser = null;
                break;
            case R.id.settings_personal:    // 编辑个人资料
                newActivity(EditPersonalInfoActivity.class);
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case ACCOUNT:   // 账号管理
                newActivity(AccountManageActivity.class);
                break;
            case PRIVATE:   // 隐私设置
                newActivity(PrivateSettingsActivity.class);
                break;
            case NOTIFY:    // 通知
                newActivity(NotificationActivity.class);
                break;
            case ORDER:     // 我的订单
                newActivity(MyOrdersActivity.class);
                break;
            case INVITE:    // 邀请好友
                newActivity(InviteFriendsActivity.class);
                break;
            case ABOUT_US:  // 关于我们
                alert("智慧城v1.0\n智享生活，慧业文人。");
                break;
            case CHECK:     // 检查更新
                alert("已经是最新版本");
                break;
        }
    }

}
